package com.tarena.lbs.user.service.impl;

import com.alibaba.nacos.common.utils.CollectionUtils;
import com.github.pagehelper.PageInfo;
import com.tarena.lbs.base.common.utils.Asserts;
import com.tarena.lbs.base.protocol.exception.BusinessException;
import com.tarena.lbs.base.protocol.model.Result;
import com.tarena.lbs.base.protocol.pager.PageResult;
import com.tarena.lbs.user.dao.repository.UserGroupRepository;
import com.tarena.lbs.user.pojo.param.UserGroupParam;
import com.tarena.lbs.user.pojo.po.AdminPO;
import com.tarena.lbs.user.pojo.po.UserGroupPO;
import com.tarena.lbs.user.pojo.query.UserGroupQuery;
import com.tarena.lbs.user.pojo.vo.AdminVO;
import com.tarena.lbs.user.pojo.vo.UserGroupVO;
import com.tarena.lbs.user.service.UserGroupService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service
public class UserGroupServiceImpl implements UserGroupService {

    @Autowired
    private UserGroupRepository userGroupRepository;
    @Override
    public PageResult<UserGroupVO> pageList(UserGroupQuery query) {
        // 创建一个新的分页结果对象，用于存储最终的分页查询结果
        PageResult<UserGroupVO> voPage = new PageResult<>();
        //total objects pageNo pageSize totalPage
        PageInfo<UserGroupPO> pageInfo = userGroupRepository.pageList(query);
        // 将分页信息中的页码设置到分页结果对象中
        voPage.setPageNo(pageInfo.getPageNum());
        // 将分页信息中的每页数量设置到分页结果对象中
        voPage.setPageSize(pageInfo.getPageSize());
        // 将分页信息中的总记录数设置到分页结果对象中
        voPage.setTotal(pageInfo.getTotal());
        // 声明一个存储管理员视图对象的列表
        List<UserGroupVO> vos = null;
        // 从分页信息中获取管理员持久化对象列表
        List<UserGroupPO> pos = pageInfo.getList();
        // 判断持久化对象列表是否不为空
        if (CollectionUtils.isNotEmpty(pos)){
            vos=pos.stream().map(po->{
                UserGroupVO vo = new UserGroupVO();
                BeanUtils.copyProperties(po,vo);
                return vo;
            }).collect(Collectors.toList());
        }
        voPage.setObjects(vos);
        return voPage;
    }

    @Override
    public void save(UserGroupParam param){
        //获取当前用登录用户

        UserGroupPO po = assembleUserGroupPO(param);
        userGroupRepository.save(po);
    }
    @Override
    public void update(UserGroupParam param) throws BusinessException {

        userGroupRepository.update(assembleUserGroupPO(param));

        log.debug("更新人群数据成功，ID:{}", param.getId());
    }

    @Override
    public List<UserGroupVO> userActivatyList(UserGroupQuery query) {
        List<UserGroupVO> vos = null;
        List<UserGroupPO> pos = userGroupRepository.userActivatyList(query);;
        if (CollectionUtils.isNotEmpty(pos)) {
            vos = pos.stream().map(po -> {
                UserGroupVO vo = new UserGroupVO();
                BeanUtils.copyProperties(po, vo);
                return vo;
            }).collect(Collectors.toList());
        }
        return vos;
    }
    @Override
    public void changeStatus(Integer id, Integer status) throws BusinessException {
        // 校验参数
        Asserts.isTrue(id == null || id <= 0, new BusinessException("-1", "人群ID不能为空或无效"));
        Asserts.isTrue(status == null || (status != 0 && status != 1),
                new BusinessException("-1", "状态值必须为0或1"));

        // 构建更新对象
        UserGroupPO po = new UserGroupPO();
        po.setId(id);
        po.setStatus(status);

        // 调用Repository层更新状态
        userGroupRepository.update(po);
    }
    @Override
    public void delete(Integer id) throws BusinessException {
        // 1. 验证参数
        Asserts.isTrue(id == null || id <= 0, new BusinessException("-1", "人群ID不能为空或无效"));

        // 2. 调用Repository层执行假删除
        userGroupRepository.delete(id);
    }
    @Override
    public UserGroupVO detail(Integer id) throws BusinessException {
        // 1. 验证参数
        if (id == null || id <= 0) {
            throw new BusinessException("-1", "人群ID不能为空或无效");
        }
        // 2. 调用Repository层查询详情
        UserGroupPO po = userGroupRepository.findById(id);
        // 3. 校验人群是否存在
        if (po == null) {
            throw new BusinessException("-1", "人群不存在");
        }
        // 4. 转换为VO并返回
        UserGroupVO vo = new UserGroupVO();
        BeanUtils.copyProperties(po, vo);
        return vo;
    }




    private UserGroupPO assembleUserGroupPO(UserGroupParam param) {
        UserGroupPO po=new UserGroupPO();
        BeanUtils.copyProperties(param,po);

        if(param.getId()==null){
            po.setStatus(0);
        }
        //注册时间
        po.setCreateAt(new Date());
        return po;
    }


}
